package com.fast.develop.management.resource.compoment;

import cn.hutool.core.util.StrUtil;
import com.fast.develop.core.io.ResourceManager;
import com.fast.develop.management.ResourceTreeManager;
import com.fast.develop.management.SchemaManager;
import com.fast.develop.management.resource.emuns.ResourceType;
import com.fast.develop.management.resource.schema.DefaultSchemaManager;
import com.fast.develop.management.resource.utils.SchemaFileUtil;
import com.fast.develop.management.resource.vo.ResourceVo;
import com.fast.develop.management.resource.vo.TreeVo;
import com.fast.develop.meta.base.MetaConfiguration;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 莫寅
 * @createTime 2022-06-12 13:21
 * @desc：
 */
public class RepositoryResourceTreeManager implements ResourceTreeManager {


    private ResourceManager resourceManager;
    private TreeReader treeReader;
    private SchemaReader schemaReader;
    private SchemaRemover schemaRemover;
    private FileCreator fileCreator;
    private SchemaManager schemaManager;
    private SchemaWriter schemaWriter;

    public RepositoryResourceTreeManager(MetaConfiguration metaConfiguration, ResourceManager resourceManager) {
        this.resourceManager = resourceManager;
        this.schemaReader = new SchemaReader(resourceManager);
        this.treeReader = new TreeReader(resourceManager, schemaReader);
        this.schemaRemover = new SchemaRemover(resourceManager);
        this.fileCreator = new FileCreator(resourceManager);
        this.schemaWriter = new SchemaWriter(metaConfiguration, resourceManager);
        this.schemaManager = new DefaultSchemaManager(schemaWriter);
    }

    @Override
    public List<TreeVo<ResourceVo>> getResourceTree(){
        Collection<String> fileNames = resourceManager.getFileNames();
        Collection<String> folderNames = resourceManager.getFolderNames().stream().sorted().collect(Collectors.toList());


//        TreeVo<ResourceVo> dataTree = getSystemResource("system_repository", "Repository", ResourceType.system_repository, true);
//        icon-develop

        TreeVo<ResourceVo> appTree = getSystemResource("app_type", "快速开发", ResourceType.app_type, true);



        TreeVo<ResourceVo> domainTree = getSystemResource("domain_type", "Domain", ResourceType.domain_type, true);
        if(domainTree.getChildren() == null){
            domainTree.setChildren(new ArrayList<>());
        }
        domainTree.getChildren().addAll(treeReader.get(folderNames, fileNames, ResourceType.domain_folder));

        if(appTree.getChildren() == null){
            appTree.setChildren(new ArrayList<>());
        }
        appTree.getChildren().add(domainTree);
//        dataTree.getChildren().addAll(Arrays.asList(domainTree));

        return Arrays.asList(appTree);
    }

    @Override
    public boolean isAvailable(final String pResourceName){
        return resourceManager.isAvailable(pResourceName);
    }

    @Override
    public String read(String path){
        return schemaReader.read(path);
    }


    private TreeVo<ResourceVo> getSystemResource(String id, String title, ResourceType resourceType, boolean spread){
        TreeVo<ResourceVo> treeDto = new TreeVo();
        treeDto.setId(id);
        treeDto.setTitle(title);
        treeDto.setSpread(spread);
        treeDto.setIconSkin(resourceType.getIcon());

        ResourceVo rootResource = new ResourceVo();
        rootResource.setType(resourceType);
        rootResource.setName(title);
        rootResource.setPath("");
        treeDto.setData(rootResource);

        return treeDto;
    }

    @Override
    public void createFolder(String path){
        fileCreator.createFolder(path);
    }


    @Override
    public void createFile(String path){
        String suffix = SchemaFileUtil.getSuffix(path);
        if(StrUtil.isBlank(suffix)){
            path += ".json";
        }
        resourceManager.createFile(path);
    }

    @Override
    public void write(String name, String value) {
        resourceManager.createFile(name, value.getBytes());
    }


    @Override
    public String rename(String path, String newName){
        return fileCreator.rename(path, newName);
    }

    @Override
    public void remove(String path){
        schemaRemover.remove(path);
    }

    @Override
    public SchemaManager getSchemaManager() {
        return schemaManager;
    }
}
